function [Demodulated_Data] = HF_DemodSoft_PSK(Complex_Rx,Rate,Sigma,fadingSymbols,MSE_Real, MSE)
%*------------------------------ HF_DemodSoft_PSK --------------------------------------%
%
% HF_DemodSoft_PSK
%
% The fuction that de-modulate the input frame dependent on its rate, and
% hence the length of output frame length must match with the corressponding
% rate as follows:
%
%
%           Rate            |           Input Frame Length
%
%           3200           |                    512
%           4800           |                    768
%
%
%*------------------------------------------------------------------------------------%
% Sigma = 1;
global LUT_PSK_Q_4

if Sigma == 0
    Sigma = 1;
end

if (Rate == 3200) %QPSK
    % Phase compensation by pi/4 phase shift
    %---------------------------------------
%     Complex_Rx_Shifted = Complex_Rx*exp(1i*pi/4*1);
    
    % Soft value calculation for QPSK
    %-------------------------------
    
    bit_n1_0 = LUT_PSK_Q_4([1,2]);  %LSB
    bit_n1_1 = LUT_PSK_Q_4([3,4]);  %LSB
    
    bit_n2_0 = LUT_PSK_Q_4([1,4]);
    bit_n2_1 = LUT_PSK_Q_4([2,3]);
    
    for nn= 1:length(Complex_Rx)
        
        Y = Complex_Rx(nn);
        h = fadingSymbols(nn);
%         h= 1/ MSE;
%           h = 1;
        LLR(1,nn)  = -h*(min( abs(Y-bit_n1_1).^2) - min( abs(Y-bit_n1_0).^2));
        
        LLR(2,nn)  = -h*(min( abs(Y-bit_n2_1).^2) - min( abs(Y-bit_n2_0).^2));
        
        
%         bit_n1_num = sum(exp( (-1/Sigma^2) * (abs(Y-bit_n1_1*1)).^2 ));
%         bit_n1_denum = sum(exp( (-1/Sigma^2) * (abs(Y-bit_n1_0*1)).^2 ));
%         LLR(1,nn) = h*log( bit_n1_num/bit_n1_denum );
%         
%         bit_n2_num = sum(exp( (-1/Sigma^2) * (abs(Y-bit_n2_1*1)).^2 ));
%         bit_n2_denum = sum(exp( (-1/Sigma^2) * (abs(Y-bit_n2_0*1)).^2 ));
%         LLR(2,nn) = h*log( bit_n2_num/bit_n2_denum );

    end
    
    if (find (abs(LLR) == inf)>0 )
        [];
    end
    
    LLR(1,find(abs(LLR(1,:)) == inf)) = sign(LLR(1,find(abs(LLR(1,:)) == inf)))*800;
    LLR(2,find(abs(LLR(2,:)) == inf)) = sign(LLR(2,find(abs(LLR(2,:)) == inf)))*800;
    
    %
%     LLR(1,:) = -imag(Complex_Rx_Shifted); %MSB
%     LLR(2,:) = -real(Complex_Rx_Shifted);  %LSB

elseif (Rate == 4800) %8PSK

    M = 3;
    % get the bit allocation Table for 8PSK 
    %-----------------------------------
    Table = getBitValLoc_8PSK(M);
    for n = 1 : length (Complex_Rx)
        z = Complex_Rx(n);
        h = fadingSymbols(n);
%         h = 1;
%         h= 1/ MSE;
        for m = 1:M
            % Soft value calculation (Log Likelihood Relation)
            %---------------------------------------------
%             bit_num = sum(exp( (-1/Sigma^2) * (abs(z-Table(2,:,m)*h)).^2 ));
%             bit_denum = sum(exp( (-1/Sigma^2) * (abs(z-Table(1,:,m)*h)).^2 ));
%             LLR(m,n) = log( bit_num/bit_denum );
            
            LLR(m,n)  = h*(min( abs(z-Table(1,:,m)).^2) - min( abs(z-Table(2,:,m)).^2));
        end
    end

    if (find (abs(LLR) == inf)>0 )
        [];
    end
    
    
    LLR(1,find(abs(LLR(1,:)) == inf)) = sign(LLR(1,find(abs(LLR(1,:)) == inf)))*800;
    LLR(2,find(abs(LLR(2,:)) == inf)) = sign(LLR(2,find(abs(LLR(2,:)) == inf)))*800;
    LLR(3,find(abs(LLR(3,:)) == inf)) = sign(LLR(3,find(abs(LLR(3,:)) == inf)))*800;
    
end


%====   LLR(1,:) is MSB
%====   LLR(M,:) is LSB
% then  flipud(LLR(1,:))  is LSB
% then  flipud(LLR(M,:))  is MSB


Demodulated_Data = reshape(flipud(LLR),1,[]);